<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="700"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :offset="3" :sm="20" :xs="24">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="标准编号">
              <a-input v-decorator="['code', {
            rules: [
              {
                required: true,
                message: '标准编号不能为空!',
              },
            ],
            }]" placeholder="请输入标准编号" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :offset="3" :sm="20" :xs="24">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="标准名称">
              <a-input v-decorator="['name', {
            rules: [
              {
                required: true,
                message: '标准名称不能为空!',
              },
            ],
            }]" placeholder="请输入标准名称" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :offset="3" :sm="20" :xs="24">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="实施日期">
              <a-date-picker
                v-decorator="['implementationDate', {initialValue: currentTime, rules: [{ required: true, message: '请选择实施日期' }] }]"
                format="yyyy-MM-DD"
                placeholder="请选择实施日期"
                showTime
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :offset="3" :sm="20" :xs="24">
            <a-form-item :labelCol="labelCol"
                         :wrapperCol="wrapperCol" label="发布日期">
              <a-date-picker
                v-decorator="['releaseDate', {initialValue: currentTime, rules: [{ required: true, message: '请选择发布日期' }] }]"
                format="yyyy-MM-DD"
                placeholder="请选择发布日期"
                showTime
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :offset="3" :sm="20" :xs="24">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="标准类型">
              <a-select v-decorator="['type', {
            rules: [
              {
                required: true,
                message: '标准类型不能为空!',
              },
            ],
            }]" placeholder="请选择标准类型" @change="handleChange">
                <a-select-option value="GUO">
                  国标
                </a-select-option>
                <a-select-option value="HANG">
                  行标
                </a-select-option>
                <a-select-option value="DI">
                  地标
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import moment from 'moment'
import { httpAction, postAction } from '@api/manage'

export default {
  name: 'standardModal',
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      currentTime: '',
      dialogStyle: {
        top: '300px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      validatorRules: {
        totalEnergyConsumption: {
          rules: [{ required: true, trigger: 'change', message: '请输入数字！', pattern: '^\d{1,}\.\d{1,}|\d{1,}]/g' }]
        }
      },
      url: {
        add: '/carbon/demonstrationPilotCarbonStandard/add',
        edit: '/carbon/demonstrationPilotCarbonStandard/edit'
      }
    }
  },
  created() {
    this.getCurrentData()
  },
  methods: {
    getCurrentData() {
      if (this.currentTime === '') {
        this.currentTime = moment().format('YYYY-MM-DD')
      }
    },
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'code', 'name', 'implementationDate', 'releaseDate', 'type', 'id'
        ))
      })
    },
    handleChange(value) {
      console.log(value)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          if (formData.implementationDate & typeof formData.implementationDate == 'object') {
            formData.implementationDate = moment(formData.implementationDate).format('YYYY-MM-DD')
          }
          if (formData.releaseDate & typeof formData.releaseDate == 'object') {
            formData.releaseDate = moment(formData.releaseDate).format('YYYY-MM-DD')
          }
          httpAction(httpurl, formData, method).then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('refresh')
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
            that.$parent.$parent.loadData()
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>